package top.smallway.UI;

import top.smallway.Myiews.PasswordField;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Login {

    private static String _user;
    private String _password;
    private int second = 59;
    public Timer timer;
    private boolean isDraging;
    private int xx, yy;
    public JLabel Clock = new JLabel("60");
    public JFrame frame = new JFrame("简易ATM");


    public Login() {
        frame.setSize(775, 502);
        frame.setUndecorated(true);
        frame.setBackground(new Color(0, 0, 0, 0));
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();  //获取屏幕对象
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int x = (int) screensize.getWidth() / 2 - frame.getWidth() / 2;
        int y = (int) screensize.getHeight() / 2 - frame.getHeight() / 2;
        frame.setLocation(x, y);
        frame.setLocationRelativeTo(null);  //自动居中对齐
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                isDraging = true;
                xx = e.getX();
                yy = e.getY();
            }


            @Override
            public void mouseReleased(MouseEvent e) {
                isDraging = false;
            }
        });
        frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDraging) {
                    int left = frame.getX();
                    int top = frame.getY();
                    frame.setLocation(left + e.getX() - xx, top + e.getY() - yy);
                }
            }
        });


    }

    public void Print() {
        Panels total = new Panels();
        total.setOpaque(false);
        placeComponents(total);
        start_timer_down();
        frame.add(total);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    }

    public ActionListener timer_down() {
        return e -> {
            Clock.setText(Integer.toString(second));
            second -= 1;
            if (second == -1) {
                JOptionPane.showMessageDialog(frame, "超时，即将退出系统", "警告", JOptionPane.ERROR_MESSAGE);
                timer.stop();
                System.exit(0);
            }
        };
    }

    public void start_timer_down() {
        MyTime myTime = new MyTime();
        new Thread(myTime).start();
    }

    class MyTime implements Runnable {
        ActionListener count_down = timer_down();

        public void run() {
            int time = 1000;
            timer = new Timer(time, count_down);
            timer.start();
        }
    }

    private void placeComponents(JPanel panel) {
        panel.setLayout(null);
        JLabel title = new JLabel("用户登录");
        title.setFont(new Font("楷体", Font.BOLD, 56));
        title.setForeground(Color.WHITE);
        title.setBounds(280, 46, 300, 100);
        panel.add(title);

        Clock.setFont(new Font("楷体", Font.BOLD, 30));
        Clock.setForeground(Color.WHITE);
        Clock.setBounds(100, 20, 100, 100);
        panel.add(Clock);


        JTextField user_name = new JTextField("银行卡号");
        user_name.setFont(new Font("楷体", Font.PLAIN, 36));
        user_name.setBounds(150, 170, 500, 60);
        panel.add(user_name);

        user_name.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (user_name.getText().equals("银行卡号")) {
                    user_name.setText("");     //将提示文字清空
                    user_name.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (user_name.getText().equals("")) {
                    user_name.setForeground(Color.gray); //将提示文字设置为灰色
                    user_name.setText("银行卡号");     //显示提示文字
                }

            }
        });

        PasswordField pass_user = new PasswordField();
        pass_user.setForeground(Color.gray); //将提示文字设置为灰色
        pass_user.setText("密码");
        pass_user.setEchoChar((char) 0);
        pass_user.setFont(new Font("楷体", Font.PLAIN, 36));
        pass_user.setBounds(150, 270, 500, 60);
        pass_user.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (String.valueOf(pass_user.getPassword()).equals("密码")) {
                    pass_user.setText("");     //将提示文字清空
                    pass_user.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (String.valueOf(pass_user.getPassword()).equals("")) {
                    pass_user.setForeground(Color.gray); //将提示文字设置为灰色
                    pass_user.setText("密码");
                    pass_user.setEchoChar((char) 0);

                }

            }
        });
        panel.add(pass_user);


        JButton confirm = new JButton("登录");
        confirm.setFont(new Font("楷体", Font.BOLD, 30));
        confirm.setBounds(150, 370, 150, 45);
        confirm.setBackground(new Color(71, 159, 254));  // 你可以选择任何颜色
        confirm.setOpaque(true);

        JButton reg = new JButton("注册");
        reg.setFont(new Font("楷体", Font.BOLD, 30));
        reg.setBounds(325, 370, 150, 45);
        reg.setBackground(new Color(71, 159, 254));  // 你可以选择任何颜色
        reg.setOpaque(true);
        panel.add(confirm);
        reg.addActionListener(e -> SwingUtilities.invokeLater(() -> {
            Register register = new Register();
            register.setVisible(true);
            register.setDefaultCloseOperation(register.EXIT_ON_CLOSE);
        }));
        panel.add(reg);
        confirm.addActionListener(e -> {
            _user = user_name.getText();
            _password = pass_user.getHashPassword();
            new Thread(() -> {
//                    TODO 用户名密码的网络请求


                SwingUtilities.invokeLater(() -> {
                    MainPage mainPage = new MainPage();
                    mainPage.setVisible(true);
                    mainPage.setDefaultCloseOperation(mainPage.EXIT_ON_CLOSE);
                });

            }).start();
        });

        JButton cancel = new JButton("退出");
        Dimension cdimension = new Dimension(80, 40);
        cancel.setPreferredSize(cdimension);   //设置按钮大小
        cancel.setFont(new Font("楷体", Font.BOLD, 30));
        cancel.setBounds(500, 370, 150, 45);
        cancel.setBackground(new Color(71, 159, 254));
        panel.add(cancel);
        cancel.addActionListener(e -> System.exit(0));


    }

    static class Panels extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage backgroundImage;
            try {
                backgroundImage = ImageIO.read(new File("src/main/resources/bj.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            // 确保 JPanel 背景是透明的
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制 PNG 图像 (确保透明部分显示)
            if (backgroundImage != null) {
                g2d.drawImage(backgroundImage, 0, 0, null);
            }
        }
    }


    public static void main(String[] args) {
        Login login = new Login();
        login.Print();


    }
}
